<?php

namespace app\admin\controller;

use app\models\ScheduleModel;

/**
 * Class Schedule.
 */
class Schedule extends Common
{
    /**
     * 上课进度模型.
     *
     * @var ScheduleModel
     */
    protected $scheduleModel;

    /**
     * 上课进度管理.
     *
     * @throws \think\exception\DbException
     *
     * @return mixed
     */
    public function lists()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $campus = $this->scheduleModel->getScheduleLists($param);
            $this->success('success', '', $campus, 0);
        }

        return $this->fetch('', [
            'course_id' => input('id'),
            'course_name' => input('course_name'),
        ]);
    }

    /**
     * 添加上课进度.
     *
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if (null !== $this->scheduleModel->save($data)) {
                $this->success('添加成功', '', '', 1);
            }
            $this->error('添加进度失败', null, '', 1);
        }

        return $this->fetch('', ['course_id' => input('course_id'),'campus_id' => input('campus_id')]);
    }

    /**
     * 编辑上课进度.
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
			$data['campus_id'] = get_current_campus_id();
            if (null !== $this->scheduleModel->save($data, ['id' => $data['id']])) {
                $this->success('编辑成功', url('Schedule/lists'), '', 1);
            }
            $this->error('编辑进度失败', null, '', 1);
        }
        $schedule = $this->scheduleModel->where(['id' => input('id')])->find();
        return $this->fetch('', [
            'course_id' => input('course_id'),
            'schedule' => $schedule
            ]);
    }

    /**
     * 删除校区.
     *
     * @return mixed
     */
    public function del()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $ids = input('ids');
            $where['id'] = ['in', $ids];
            if (!$this->scheduleModel->where($where)->delete()) {
                $this->error('删除进度失败', null, '', 1);
            }
            $this->success('删除成功', url('Schedule/lists'), '', 1);
        }
    }

    /**
     * 实例化
     * @return bool|void
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->scheduleModel = new ScheduleModel();
    }
}
